package univer.oko.database.rmi.impl;

import univer.oko.Error;

import java.util.Vector;

/**
 * @author Eduard Dudar
 *         Date: 20.11.2005
 *         Time: 13:15:29
 */
public class EducationLevelImpl extends ImplHelper {
    public EducationLevelImpl(RMIImpl rmi) {
        super(rmi);
    }

    public synchronized int addEducationLevel(String str, Vector v) {
        if (this.isOccurs("SELECT ID_EducLevel FROM Education_Level WHERE EducLevel_Name = '" + str + "'")) {
            return univer.oko.Error.REPEAT;
        }

        int maxLevel = this.getIntValue("SELECT MAX(ID_EducLevel) FROM Education_Level") + 1;
        int maxLP = this.getIntValue("SELECT MAX(ID_LtoP) FROM Level_by_Program") + 1;

        this.executeUpdate("INSERT INTO Education_Level VALUES(" + maxLevel + ",'" + str + "')");

        for (int i = 0; i < v.size(); i++) {
            int program = ((Integer) v.get(i)).intValue();
            this.executeUpdate("INSERT INTO Level_by_Program VALUES(" + (maxLP + i) + "," + maxLevel + "," + program + ")");
        }
        this.commit();
        return Error.ALRIGHT;
    }

    public synchronized Vector getEducationLevels() {
        return this.getVectorValue("SELECT EducLevel_Name FROM Education_Level ORDER BY EducLevel_Name");
    }

    public synchronized Vector getEducationLevels(String str) {
        return this.getVectorValue(
                "SELECT Education_Level.EducLevel_Name " +
                        "FROM Education_Level, Education_Program, Level_by_Program " +
                        "WHERE Education_Program.EducProg_Name = '" + str + "' " +
                        "AND Education_Program.ID_EducProg = Level_by_Program.Ref_EducProg " +
                        "AND Level_by_Program.Ref_EducLevel = Education_Level.ID_EducLevel");
    }

    public synchronized int deleteEducationLevel(String str) {
        boolean isOccurs = this.isOccurs("SELECT Education_Level.EducLevel_Name " +
                "FROM Education_Level, Student,Level_by_Program " +
                "WHERE Student.Ref_LtoP = Level_by_Program.ID_LtoP " +
                "AND Level_by_Program.Ref_EducLevel = Education_Level.ID_EducLevel " +
                "AND Education_Level.EducLevel_Name = '" + str + "'");
        if (isOccurs) {
            return Error.IN_USE;
        }

        int i = this.getEducationLevelID(str);
        this.executeUpdate("DELETE FROM Level_by_Program WHERE Ref_EducLevel = " + i);
        this.executeUpdate("DELETE FROM Education_Level WHERE EducLevel_Name = '" + str + "'");

        this.commit();
        return Error.ALRIGHT;
    }

    public synchronized Integer getEducationLevelID(String str) {
        return this.getIntValue("SELECT ID_EducLevel FROM Education_Level WHERE EducLevel_Name = '" + str + "'");
    }

    public synchronized String getEducationLevelName(int id) {
        return this.getStringValue(
                "SELECT EducLevel_Name FROM Education_Level, Level_by_Program " +
                        "WHERE ID_LtoP = " + id + "AND ID_EducLevel = Ref_EducLevel");
    }
}
